﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame
{
    public class TcpIP
    {
        public TcpClient Tcp;
        NetworkStream NetStream;
        public string IP;
        public int Port;
        public bool isConnected;
        #region 连接
        /// <summary>
        /// 【连接】
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public int Link(string ip, int port)
        {
            try
            {
                IP = ip;
                Port = port;
                Tcp = new TcpClient();
                if (!Tcp.Connected)
                {
                    Tcp.Connect(IPAddress.Parse(IP), Port);
                    NetStream = Tcp.GetStream();
                    NetStream.ReadTimeout = 50000;
                    NetStream.WriteTimeout = 50000;
                    if (Tcp.Connected)
                    {
                        isConnected = true;
                        return 0;

                    }
                    isConnected = false;
                    return 1;
                }
                isConnected = true;
                return 0;
            }
            catch (Exception ex)
            {
                isConnected = false;
                displalyInfo.displayInfoMain("连接失败:" + ex.Message + ex.StackTrace);
                MessageBox.Show("连接失败:" + ex.Message + ex.StackTrace);
                return 1;
            }
        }
        #endregion
        #region 获取数据
        /// <summary>
        /// 【获取数据】
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="outtime">超时</param>
        /// <returns></returns>
        public int GetData(string command, out string resdata, out string Msg, short outtime = 5000, bool EnterEN = false)
        {
            Msg = "";
            resdata = "";
            int IsCheck = 1;
            try
            {
                byte[] ReadBuff = new byte[512];
                if (!Tcp.Connected)
                {
                    Link(IP, Port);
                }
                if (Tcp.Connected)
                {
                    if (NetStream.DataAvailable)
                    {
                        NetStream.Read(ReadBuff, 0, 512);
                        resdata = "";
                    }
                    DateTime t1 = DateTime.Now;
                    if (EnterEN)
                        command += "\r\n";
                    NetStream.Write(Encoding.ASCII.GetBytes(command), 0, command.Length);
                    displalyInfo.displayInfoMain("发送到相机通信命令:" + command);
                    while ((DateTime.Now - t1).TotalMilliseconds < outtime)
                    {
                        if (NetStream.DataAvailable)
                        {
                            NetStream.Read(ReadBuff, 0, 512);
                            resdata = Encoding.Default.GetString(ReadBuff).Replace("\r\n", "").Replace("\r", "").Replace("\n", "").Trim();
                            IsCheck = 0;
                            break;
                        }
                        Thread.Sleep(100);
                    }
                }
                displalyInfo.displayInfoMain("获取到相机通信命令数据:" + Encoding.Default.GetString(ReadBuff));
                ReadBuff = null;
                return IsCheck;
            }
            catch (Exception ex)
            {
                Msg = "读取数据失败:" + ex.Message;
                displalyInfo.displayInfoMain("获取相机数据失败:" + ex.Message + ex.StackTrace);
                
                return 1;
            }
        }

        /// <summary>
        /// 获取视觉返回数据
        /// </summary>
        /// <param name="resdata"></param>
        /// <param name="Msg"></param>
        /// <param name="outtime"></param>
        /// <returns></returns>
        public int GetData(out string resdata, out string Msg, short outtime = 5000)
        {
            Msg = "";
            resdata = "";
            int IsCheck = 1;
            try
            {
                byte[] ReadBuff = new byte[512];
                if (!Tcp.Connected)
                {
                    Link(IP, Port);
                }
                if (Tcp.Connected)
                {
                    DateTime t1 = DateTime.Now;
                    while ((DateTime.Now - t1).TotalMilliseconds < outtime)
                    {
                        if (NetStream.DataAvailable)
                        {
                            NetStream.Read(ReadBuff, 0, 512);
                            resdata = Encoding.Default.GetString(ReadBuff).Replace("\r\n", "").Replace("\r", "").Replace("\n", "").Trim();
                            IsCheck = 0;
                            break;
                        }
                        Thread.Sleep(100);
                    }
                }
                displalyInfo.displayInfoMain("获取到相机通信命令数据:" + Encoding.Default.GetString(ReadBuff));
                ReadBuff = null;
                return IsCheck;
            }
            catch (Exception ex)
            {
                Msg = "读取数据失败:" + ex.Message;
                displalyInfo.displayInfoMain("获取相机数据失败:" + ex.Message/* + ex.StackTrace*/);
                return 1;
            }
        }
        #endregion

        #region 发送数据
        /// <summary>
        /// 【发送数据】
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="outtime">超时</param>
        /// <returns></returns>
        public int SendData(string command, bool EnterEN = false)
        {
            try
            {
                byte[] ReadBuff = new byte[512];
                if (!Tcp.Connected)
                {
                    Link(IP, Port);
                }
                if (Tcp.Connected)
                {
                    if (NetStream.DataAvailable)
                    {
                        NetStream.Read(ReadBuff, 0, 512);
                    }
                    DateTime t1 = DateTime.Now;
                    if (EnterEN)
                        command += "\r\n";
                    NetStream.Write(Encoding.ASCII.GetBytes(command), 0, command.Length);
                }
                displalyInfo.displayInfoMain("发送到相机命令:" + command);
                ReadBuff = null;
                return 0;
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain("发送到相机命令失败:" + ex.Message + ex.StackTrace);
                return 1;
            }
        }

        #endregion

        public int Close()
        {
            try
            {
                if (NetStream != null)
                {
                    NetStream.Close();
                }
                if (Tcp != null)
                {
                    Tcp.Close();
                }
                isConnected = false;
                return 0;
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain("关闭连接失败:" + ex.Message + ex.StackTrace);
                return 1;
            }
        }
    }
}
